﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ajeelee.Core.Utils;
using BoCaiWeb.Business;
using BoCaiWeb.Model;
using BoCaiWeb.WebSite.Code;

namespace BoCaiWeb.WebSite.WebAdmin
{
    public partial class TeamEdit : AuthPageBase
    {
        protected int id;

        protected ManagementTeam teamInfo = new ManagementTeam();
        protected IList<NewsClass> teamClassList = new List<NewsClass>();

        IManagementTeamBusiness teamBll = new ManagementTeamBusinessFactory().CreateManagementTeamBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            id = QueryParamInt32("id");
            try
            {
                teamClassList = Utility.GetNewsClass().Where(c => c.PId == 2).ToList();

                if (id > 0)
                {
                    teamInfo = teamBll.GetById(id);
                    if (teamInfo == null)
                    {
                        teamInfo = new ManagementTeam();
                        MessageBoxGo("不存在该信息", FormParam("sourceurl", "TeamList.aspx"));
                    }
                }
                if (IsPost)
                {
                    if (id > 0)   // 更新
                    {
                        initModel();
                        teamBll.Update(teamInfo);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo(FormParam("sourceurl", "TeamList.aspx"), true);

                    }
                    else          // 新建
                    {
                        initModel();
                        teamBll.Insert(teamInfo);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo(FormParam("sourceurl", "TeamList.aspx"), true);
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }

        public void initModel()
        {
            // 就初始首页xml
            if (id <= 0)
            {
                teamInfo = new ManagementTeam();
                teamInfo.Author = User.Identity.Name;
                teamInfo.InTime = DateTime.Now;
                teamInfo.InIP = IPAddress;
                teamInfo.State = 2;
                teamInfo.Count = 0;
            }
            teamInfo.Name = FormParam("title");
            teamInfo.Office = FormParam("office");
            teamInfo.CId = FormParamInt32("cateid");
            teamInfo.Pic = FormParam("faceImg");
            teamInfo.Content = FormParam("content");
            teamInfo.Top = FormParam("top");

            teamInfo.UpAuthor = User.Identity.Name;
            teamInfo.UpTime = DateTime.Now;
            teamInfo.UpIP = IPAddress;
        }
    }
}